package com.liyuxiang.handler.security;

import com.alibaba.fastjson.JSON;
import com.liyuxiang.utils.WebUtils;
import com.liyuxiang.vm.ResponseResult;
import com.liyuxiang.vm.enums.ResultStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 认证失败处理器
 */
@Component
@Slf4j
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        log.error("认证失败");
        authException.printStackTrace();
        ResponseResult responseResult = ResponseResult.errorResult(ResultStatusEnum.NEED_LOGIN);
        WebUtils.renderString(response, JSON.toJSONString(responseResult));
    }
}
